package dao

import (
	"github.com/jmoiron/sqlx"
	"vpn-management-server/config"
	"vpn-management-server/logger"
)

var GMysqlDB MysqlDB

type MysqlDB struct {
	Conn *sqlx.DB
}

func NewDBConn(c config.MysqlConfig) *sqlx.DB {
	logger.Logger.Debugf("connect the data base(name:%s,password:%s)", c.Name, c.Password)
	var dsn = c.Name + ":" + c.Password + "@tcp(" + c.IP + ":" + c.Port + ")/management?charset=utf8"
	db := sqlx.MustConnect("mysql", dsn) // 连接并测试连通性 无法连通 panic
	db.SetMaxOpenConns(80)               // 设置为0表示对打开连接数不做限制
	db.SetMaxIdleConns(20)               // 最多同时有10个连接空闲状态
	logger.Logger.Info("database connect success")
	return db
}

func (m *MysqlDB) Init(config *config.VPNConfig) {
	m.Conn = NewDBConn(config.MysqlConfig)
}
